<?php
/**
 * Created by PhpStorm.
 * User: zhaoyi
 * Date: 2022/1/26
 * Time: 11:46.
 */

declare(strict_types=1);

namespace App\Application\Api\Controller;

use App\Annotation\Api;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminSettingService;
use App\Application\Admin\Service\CityService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;

#[Controller("/api/store")]
class StoreController extends AbstractController
{
    #[Inject]
    protected AdminSettingService $setting;

    #[Api]
    #[PostMapping("info")]
    function info()
    {
        $store_id = $this->request->input('store_id', '');
        $city_list = CityService::getInstance()
            ->getAllcity();
        $store = Store::where('store_id', $store_id)->select('*')
            ->first() ?: [];
        return compact('city_list', 'store');
    }

    /*
     *
     *首页门店列表
     * */
    #[Api]
    #[GetMapping("lists")]
    function lists()
    {
        $where = [];
        $store_name = $this->request->input('store_name', '');
        $lat = $this->request->input('lat', '0');
        $lng = $this->request->input('lng', '0');
        $city = $this->request->input('city_code', '');

        if ($store_name != '') {
            $where[] = ['store_name', 'like', '%' . $store_name . '%'];
        }
        $region_id = Db::table('region')->where('merger_name', 'like', '%' . $city . '%')->pluck('id');
        $lists = Store::where($where)->whereIn('city_id', $region_id)
            ->select(['*', DB::raw("(6371 * acos(cos(radians($lat))  
        * cos(radians(latitude))  
        * cos(radians(longitude) - radians($lng))  
        + sin(radians($lat))  
        * sin(radians(latitude)))) AS distance")])
            ->orderBy('distance')
            ->orderBy('status')
            ->paginate()->toArray();
        foreach ($lists['data'] as $k => $v) {
            $lists['data'][$k]['city_value'] = Db::table('region')->where('id', $v['city_id'])->value('merger_name');
            //经纬   经纬
            // $lists['data'][$k]['distance'] = self::distance((float)$v['latitude'], (float)$v['longitude'], (float)$lat, (float)$lng);
            $lists['data'][$k]['distance'] = (float)round($v['distance'], 2);

        }
        return compact('lists','listsql');
    }

    /*
 *
 *门店类型列表
 * */
    #[Api]
    #[GetMapping("typelists")]
    function typelists()
    {
        $where = [];
        $store_name = $this->request->input('store_name', '');
        $lat = $this->request->input('lat', '0');
        $lng = $this->request->input('lng', '0');
        $city = $this->request->input('city_code', '');
        $store_type = $this->request->input('store_type', '1');
        if ($store_name != '') {
            $where[] = ['store_name', 'like', '%' . $store_name . '%'];
        }
        if ($store_type != '') {
            $where[] = ['store_type', '=', $store_type];
        }
        $region_id = Db::table('region')->where('merger_name', 'like', '%' . $city . '%')->pluck('id');
        $lists = Store::where($where)->whereIn('city_id', $region_id)
            ->select(['*', DB::raw("(6371 * acos(cos(radians($lat))  
        * cos(radians(latitude))  
        * cos(radians(longitude) - radians($lng))  
        + sin(radians($lat))  
        * sin(radians(latitude)))) AS distance")])
            ->orderBy('distance')
            ->orderBy('status')
            ->paginate()->toArray();

        foreach ($lists['data'] as $k => $v) {
            $lists['data'][$k]['city_value'] = Db::table('region')->where('id', $v['city_id'])->value('merger_name');
            //经纬   经纬
            // $lists['data'][$k]['distance'] = self::distance((float)$v['latitude'], (float)$v['longitude'], (float)$lat, (float)$lng);
            $lists['data'][$k]['distance'] = (float)round($v['distance'], 2);

        }
        return compact('lists');
    }

    /*
     *
     *门店下拉
     * */
    #[Api]
    #[GetMapping("select_lists")]
    function select_lists()
    {
        $where = [];
        //  $where[] = ['status', '=', 1];
        $lists = Store::where($where)->select(['store_name', 'store_id', 'head_url', 'address', 'city_id', 'mobile'])->get()->toArray();
        foreach ($lists as $k => $v) {
            $lists[$k]['address'] = Db::table('region')->where('id', $v['city_id'])->value('merger_name') . '' . $lists[$k]['address'];
            $lists[$k]['head_url'] = json_decode($lists[$k]['head_url']);
        }
        return compact('lists');
    }

    /*
 *
 *门店管理下拉
 * */
    #[Api]
    #[GetMapping("adminstore")]
    function adminstore()
    {
        $where = [];
        $user = $this->checkToken();
        $where[] = ['member_id', '=', $user->member_id];
        $lists = Store::where($where)->select(['store_name', 'store_id'])->get()->toArray();
        return compact('lists');
    }


}